package com.diamond.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;

import com.diamond.dao.GenericDAO;
import com.diamond.service.IService;

@SuppressWarnings("unchecked")
public class Service<T> implements IService<T> {
	public GenericDAO genericDAO;

	public void setGenericDAO(GenericDAO genericDAO) {
		this.genericDAO = genericDAO;
	}

	public boolean deleteByParam(Serializable[] args) throws Exception {
		for (int i = 0; i < args.length; i++) {
			genericDAO.deleteByKey(args[i]);
		}
		return true;
	}

	public void save(Object entity) throws Exception {
		genericDAO.save((Serializable) entity);
	}

	public void update(Object entity) throws Exception {
		genericDAO.update((Serializable) entity);
	}

	public Object findById(Serializable id) throws Exception {
		return genericDAO.findById(id);
	}

	public List findByCriteria(int firstResult, int maxResults,DetachedCriteria criteria) throws Exception {
		return genericDAO.findByCriteria(criteria, (firstResult - 1)* maxResults, maxResults);
	}

	public Long getRowCount(String HQL) throws Exception {
		return genericDAO.getRowCount(HQL);
	}

	public List<T> findByHQL(int currPage, int pagesize, String hql) {
		return genericDAO.findByHQL(currPage, pagesize, hql);
	}
	
	public List<T> findByHQL(final String hql) {
		return genericDAO.findByHQL(hql);
	}
	
	public List<T> find(final String hql,Object[] values) {
		return genericDAO.find(hql, values);
	}

	@Override
	public List<T> findBySqlToObj(String SQL, Class<T> cls) {
		return genericDAO.findBySqlToObj(SQL, cls);
	}

	@Override
	public void deleteByKey(Serializable id) throws Exception {
		genericDAO.deleteByKey(id);
		
	}

	@Override
	public Map findPage(int currPage, int pagesize, String hql) {
		return genericDAO.findPage(currPage,pagesize,hql);
	}

	@Override
	public Map findCountAndPageBySql(int currPage, int pagesize,String SQL, Class<T> cls) {
		return genericDAO.findCountAndPageBySql(currPage,pagesize,SQL,cls);
	}

	@Override
	public Object merge(Object entity) throws Exception{
		return genericDAO.merge((Serializable) entity);
	}

	@Override
	public List<T> findBySQL(String sql){
		return genericDAO.findByNHQL(sql);
	}

	@Override
	public void updateBySQL(String sql) {
		genericDAO.updateBySql(sql);
	}

}
